package com.stlm2.controller;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.user.entity.Lower;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.user.service.LowerService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：fuchunyu
 * Date：2017/9/12
 * Description：
 */
@Controller
@RequestMapping("/lowerC")
public class LowerController {

    @Resource
    private UserDao userDao;

    @Resource
    private LowerService lowerService;

    @ResponseBody
    @RequestMapping("/shareTeam.json")
    public String shareTeam(String companyCode,String token){
        User user = userDao.getByToken(token);
        if (user==null)
            return CodeContent.returnLogin();

        List<Lower> oneLower=lowerService.getOneLower(user.getId());
        List<Lower> twoLower=lowerService.getTwoLower(user.getId());
        List<Lower> threeLower=lowerService.getThreeLower(user.getId());

        double oneLowerNumber=oneLower.size();
        double twoLowreNumber=twoLower.size();
        double treeLowerNumber=threeLower.size();
        double totalLowerNumber=oneLowerNumber+twoLowreNumber+treeLowerNumber;
        double oneLowerPercentage=0;
        double twoLowerPercentage=0;
        double threeLowerPercentage=0;


        if(totalLowerNumber!=0){
            oneLowerPercentage = oneLowerNumber / totalLowerNumber;
            twoLowerPercentage = twoLowreNumber / totalLowerNumber;
            threeLowerPercentage = treeLowerNumber / totalLowerNumber;
        }

        ModelMap one=new ModelMap();
        one.put("percentage",oneLowerPercentage);
        one.put("persons",oneLowerNumber);
        ModelMap two=new ModelMap();
        two.put("percentage",twoLowerPercentage);
        two.put("persons",twoLowreNumber);
        ModelMap three=new ModelMap();
        three.put("percentage",threeLowerPercentage);
        three.put("persons",treeLowerNumber);
        ModelMap map=new ModelMap();
        map.put("one",one);
        map.put("two",two);
        map.put("three",three);

        return CodeContent.returnSuccess(map);

    }
    @ResponseBody
    @RequestMapping(value = "/lookLower.json",produces="application/json;charset=UTF-8")
    public String lookLower(String companyCode,String token,String grade,String page){
        User user=userDao.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();
        List<Lower> list=null;
        int pageNo = 1;
        int pageSize=20;
        if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
            pageNo = NumberUtils.toInt(page);
        }
        PageView pageView=new PageView();
        Map<String,Object> map=new HashMap<String ,Object>();
        map.put("userId",user.getId());
        if("1".equals(grade)){
            map.put("grade",grade);
             pageView=lowerService.getpage2(pageNo,pageSize,"queryLower",map);
        } else if("2".equals(grade)){
            map.put("grade",grade);
             pageView=lowerService.getpage2(pageNo,pageSize,"queryLower",map);
        } else if ("3".equals(grade)){
            map.put("grade",grade);
             pageView=lowerService.getpage2(pageNo,pageSize,"queryLower",map);
        } else{
            CodeContent.returnFailure("请检查级别");
        }
        Map<String,Object> result=new HashMap<String,Object>();
        result.put("data",lowerService.convent(pageView.getRecords()));
        result.put("recordsTotal", pageView.getRowCount());
        result.put("totalPage", pageView.getPageCount());
        return CodeContent.returnSuccess(result);
    }
}
